module Hexena.Internal.Minimiser
  ( OrderList
  , randomiseOList
  , reorderedCharacteristicMatrix
  ) where

import GSL.Random.Gen
import Data.Packed.Matrix
import Data.Packed.Vector

-- | Vector with characteristics of a malware. May be anything and
-- | take care of proper normalisation.
type CharacteristicVector = Vector Double
-- | A matrix of charactersitic vectors. Each vector is in a row.
type CharacteristicMatrix = Matrix Double
-- | Order of the rows for rearrangement
type OrderList = [Int]

reorderedCharacteristicMatrix :: CharacteristicMatrix -> OrderList -> CharacteristicMatrix
reorderedCharacteristicMatrix m o = extractRows o m

randomiseOList :: RNG -> OrderList -> IO OrderList
randomiseOList rng ols = (rF ols . ranLim) `fmap` mapM id (replicate nLen $ getUniform rng)
                         --do r <- mapM id $ replicate nLen (getUniform rng)
                         --   return $ rF (ranLim r) ols
    where nLen = length ols
          nLens = [nLen,nLen-1..]
          ranLim = map (\(y, x) -> floor (x * fromIntegral y)) . zip nLens
          rF [] _ = []
          rF _ [] = error "Guru Meditation! Random numbers used up!"
          rF ol (r:rs) = let (b, e:es) = splitAt r ol
                         in e : rF (es ++ b) rs